package com.carkeeper.system.service.impl;

import com.carkeeper.common.utils.PageResult;
import com.carkeeper.common.utils.Result;
import com.carkeeper.common.utils.StatusCode;
import com.carkeeper.system.dao.DepartmentDao;
import com.carkeeper.system.dao.EmployeeDao;
import com.carkeeper.system.dao.UserDao;
import com.carkeeper.system.feign.DepartmentFeign;
import com.carkeeper.system.pojo.Department;
import com.carkeeper.system.pojo.Employee;
import com.carkeeper.system.service.DepartmentService;
import com.carkeeper.system.service.EmployeeService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao employeeDao;
    @Autowired
    private DepartmentService departmentService;

    /**
     * 增加
     *
     * @param employee
     */
    @Override
    public void add(Employee employee) {
        employeeDao.insert(employee);
    }


    /**
     * 根据ID查询
     * 可用于修改时数据回显
     *
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findById(Integer id) {
        Map<String, Object> resultMap = new HashMap<>();
        Employee employee = employeeDao.selectByPrimaryKey(id);
        List<Department> departments = departmentService.findAll();
        resultMap.put("departments", departments);
        resultMap.put("employee", employee);
        return resultMap;

    }

    /**
     * 修改
     *
     * @param employee
     */
    @Override
    public void update(Employee employee) {
        employeeDao.updateByPrimaryKey(employee);
    }

    /**
     * 锁定/解锁
     *
     * @param employee
     */
    @Override
    public Result lock(Employee employee) {
        Result<Object> result = new Result<>();
        //锁定
        if ("0".equals(employee.getAccountStatus())) {
            employee.setAccountStatus("1");
            employeeDao.updateByPrimaryKey(employee);
            return new Result(true, StatusCode.OK, "锁定成功");
        } else {
            //解锁
            employee.setAccountStatus("0");
            employeeDao.updateByPrimaryKey(employee);
            return new Result(true, StatusCode.OK, "解锁成功");
        }
    }


    /**
     * 条件+分页查询
     *
     * @param searchMap 查询条件
     * @param page      页码
     * @param size      页大小
     * @return 分页结果
     */
    @Override
    public Map<String, Object> findPage(Map<String, Object> searchMap, int page, int size) {
        Map<String, Object> resultMap = new HashMap<>();
        List<Department> departments = departmentService.findAll();
        Example example = createExampleForEmployee(searchMap);
        PageHelper.startPage(page, size);
        Page<Employee> pageList = (Page<Employee>) employeeDao.selectByExample(example);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        resultMap.put("pageResult", pageResult);
        resultMap.put("departments", departments);
        return resultMap;
    }

    /**
     * 构建查询对象
     * 员工查询
     *
     * @param searchMap
     * @return
     */
    private Example createExampleForEmployee(Map<String, Object> searchMap) {
        Example example = new Example(Employee.class);
        Example.Criteria criteria = example.createCriteria();
        if (searchMap != null) {
            // 姓名
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name"))) {
                criteria.andLike("name", "%" + searchMap.get("name") + "%");
            }
            // 手机号
            if (searchMap.get("phone") != null && !"".equals(searchMap.get("phone"))) {
                criteria.andLike("phone", "%" + searchMap.get("phone") + "%");
            }
            // 电子邮件
            if (searchMap.get("email") != null && !"".equals(searchMap.get("email"))) {
                criteria.andLike("email", "%" + searchMap.get("email") + "%");
            }
            // 部门id
            if (searchMap.get("departmentId") != null && !"".equals(searchMap.get("departmentId"))) {
                criteria.andLike("departmentId", "%" + searchMap.get("departmentId") + "%");
            }
            // 状态
            if (searchMap.get("accountStatus") != null && !"".equals(searchMap.get("accountStatus"))) {
                criteria.andLike("accountStatus", "%" + searchMap.get("accountStatus") + "%");
            }
            // id
            if (searchMap.get("id") != null) {
                criteria.andEqualTo("id", searchMap.get("id"));
            }
        }
        return example;
    }
}
